package com.ctz.commonframe.ui.widget;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.ctz.commonframe.R;
import com.ctz.commonframe.entity.ImageItem;
import com.ctz.commonframe.ui.BaseActivity;
import com.ctz.commonframe.utils.FilePathUtil;


public class FileChooseActivity extends BaseActivity {
	private ListView listView;
	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> sdCardRootList = new ArrayList<Map<String, Object>>();
	private FileChooseAdapter adapter;
	private String mDir;
	private Dev_MountInfo.DevInfo info;
	private boolean isRoot;
	private TextView tvTitle, tvCancel;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_filechoose);
		initWidget();
		getSDCardPath();
		list.addAll(sdCardRootList);
		adapter = new FileChooseAdapter(this, list);
		listView.setAdapter(adapter);
	}

	private void initWidget() {
		listView = (ListView) findViewById(R.id.fileList);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		tvCancel = (TextView) findViewById(R.id.tvCancel);

		tvCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if ((Integer) list.get(position).get("type") == 1 || (Integer) list.get(position).get("type") == 3) {
					mDir = (String) list.get(position).get("path");
					isRoot = (Boolean) list.get(position).get("isRoot");
					String name = (String) list.get(position).get("title");
					tvTitle.setText(name);
					list.clear();
					list.addAll(getData());
					List<Map<String, Object>> willRemoveList = new ArrayList<Map<String, Object>>();
					for (int i = 0; i < list.size(); i++) {
						Map<String, Object> map = list.get(i);
						String title = (String) map.get("title");

						if (map.get("size") != null) {
							long size = (Long) map.get("size");
							if (size == 0 || title.indexOf(".") == -1) {
								willRemoveList.add(map);
							}
						}

					}
					list.removeAll(willRemoveList);
					adapter.notifyDataSetChanged();

				} else {
					finishWithResult((String) list.get(position).get("title"), (String) list.get(position).get("path"),
							String.valueOf(list.get(position).get("size")));
				}
			}

			private void finishWithResult(String name, String path, String size) {
				ImageItem item = new ImageItem();
				if (isPicture(name)) {
					item.imageName = name;
					item.size = size;
					item.imagePath = path;
					item.fileType = 1;
				} else {
					item.imageName = name;
					item.size = size;
					item.imagePath = path;
					item.fileType = 2;
				}
				Intent intent = new Intent();
				intent.putExtra("file_data", item);
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}

	private void getSDCardPath() {
		Dev_MountInfo dev = Dev_MountInfo.getInstance();
		info = dev.getExternalInfo();// External SD Card Informations
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("title", "sdcard1");
		map1.put("path", FilePathUtil.getRootPath());
		map1.put("pic", R.drawable.file_folder);
		map1.put("type", 1);
		map1.put("isRoot", false);
		sdCardRootList.add(map1);
		if (info != null) {
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("title", info.getLabel());
			map2.put("path", info.getPath());
			map2.put("pic", R.drawable.file_folder);
			map2.put("type", 1);
			map2.put("isRoot", false);
			sdCardRootList.add(map2);
		}
	}

	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		File f = new File(mDir);
		File[] files = f.listFiles();
		if (files != null) {
			if (!isRoot) {
				map = new HashMap<String, Object>();
				if (info != null) {
					if (mDir.equals(info.getPath()) || mDir.equals(FilePathUtil.getRootPath())) {
						map.put("isRoot", true);
					} else {
						map.put("isRoot", false);
					}
				} else {
					if (mDir.equals(FilePathUtil.getRootPath())) {
						map.put("isRoot", true);
					} else {
						map.put("isRoot", false);
					}
				}

				map.put("path", f.getParent());
				map.put("title", f.getParentFile().getName());
				map.put("pic", R.drawable.file_back);
				map.put("type", 3);
				newList.add(map);
				for (int i = 0; i < files.length; i++) {
					map = new HashMap<String, Object>();
					String fileName = files[i].getName().substring(0, 1);
					if (!fileName.equals(".")) {
						map.put("isRoot", false);
						map.put("title", files[i].getName());
						map.put("path", files[i].getPath());
						if (files[i].isDirectory()) {
							map.put("pic", R.drawable.file_folder);
							map.put("type", 1);
						} else {
							if (isPicture(files[i].getName())) {
								map.put("pic", R.drawable.file_pic);
							} else {
								map.put("pic", R.drawable.file_other);
							}
							map.put("type", 2);
							map.put("size", files[i].length());
							map.put("date", files[i].lastModified());
						}
						newList.add(map);
					}
				}
			} else {
				newList.addAll(sdCardRootList);
				tvTitle.setText("");
			}
		}

		return newList;
	}

	private boolean isPicture(String name){
		boolean isPic = false;
		if(name.endsWith(".jpg") ||name.endsWith(".jpeg")||name.endsWith(".gif")||name.endsWith(".png")||name.endsWith(".bmp")||name.endsWith(".wbmp")){
			isPic = true;
		}
		return isPic;
	}
}
